package com.org.qrtrace.controller;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.org.qrtrace.Entry.request.CheckHistoryRequest;
import com.org.qrtrace.pojo.Checkhistory;
import com.org.qrtrace.pojo.Phistory;
import com.org.qrtrace.service.ICheckhistoryService;
import com.org.qrtrace.service.IPhistoryService;
import com.org.qrtrace.utils.CommonUtils;
import com.org.qrtrace.utils.JsonResult;
import com.org.qrtrace.utils.QRCodeUtil;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bonnie
 * @since 2023-03-18
 */
@RestController
@RequestMapping("/checkhistory")
@Api(tags = "检测记录api")
public class CheckhistoryController {

    private final Logger logger = LoggerFactory.getLogger(CheckhistoryController.class);

    @Autowired
    private ICheckhistoryService service;
    @Autowired
    private IPhistoryService phistoryService;

    @GetMapping("/lists")
    public JsonResult<?> getList(){
        return JsonResult.successInfo(service.list());
    }

    @PostMapping("/add")
    public JsonResult<?> addData(CheckHistoryRequest checkHistoryRequest, HttpServletRequest request){
        Checkhistory checkhistory = new Checkhistory();
        String data = JSON.toJSONString(checkHistoryRequest);
        checkhistory = JSON.parseObject(data,Checkhistory.class);
        try {
            CommonUtils.setTimeAndUser(checkhistory,request);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail("操作失败！");
        }
        checkhistory.setCuid(CommonUtils.getUUID());
        checkhistory.setCheckTime(checkhistory.getCreateTime());
        checkhistory.setChecker(checkhistory.getCreateBy());
        logger.info(JSON.toJSONString(checkhistory));

        QueryWrapper<Phistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("puid",checkHistoryRequest.getPuid());
        queryWrapper.eq("cuid",checkHistoryRequest.getCuid());
        Phistory phistory = phistoryService.getOne(queryWrapper);
        logger.info(data);
        logger.info(JSON.toJSONString(phistory));
        if(service.save(checkhistory) && phistory!=null){
            phistory.setCuid(checkhistory.getCuid());
            if(phistoryService.update(phistory,queryWrapper)){
                return JsonResult.successInfo(service.list());
            }else {
                return JsonResult.fail("操作失败！");
            }
        }else {
            return JsonResult.fail("操作失败！");
        }
    }

}
